package cn.estore.dao;

import cn.estore.entity.Product;
import cn.estore.util.DBConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDao {

	private DBConnection jdbc;

	public ProductDao() {
		jdbc = new DBConnection();
	}

	// 私有方法，用于从结果集填充Product对象并添加到列表中
	private void fillProductListFromResultSet(ResultSet rs, List<Product> list) throws SQLException {
		while (rs.next()) {
			Product e = new Product();
			e.setId(rs.getInt(1));
			e.setName(rs.getString(2));
			e.setProducingArea(rs.getString(3));
			e.setDescription(rs.getString(4));
			e.setCreateTime(rs.getString(5));
			e.setMarketPrice(rs.getDouble(6));
			e.setSellPrice(rs.getDouble(7));
			e.setProductAmount(rs.getInt(8));
			e.setPicture(rs.getString(9));
			e.setDiscount(rs.getInt(10));
			list.add(e);
		}
	}

	// 查询所有商品记录的方法
	public List<Product> selectAllProducts() {
		List<Product> list = new ArrayList<>();
		Connection conn = jdbc.getConnection();
		try (PreparedStatement stmt = conn.prepareStatement("select * from product");
			 ResultSet rs = stmt.executeQuery()) {
			fillProductListFromResultSet(rs, list);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	public Product selectProductById(int productId) {
		Product product = null;
		Connection conn = jdbc.getConnection();
		try (PreparedStatement stmt = conn.prepareStatement("select * from product where id =?")) {
			stmt.setInt(1, productId);
			try (ResultSet rs = stmt.executeQuery()) {
				if (rs.next()) {
					product = new Product();
					product.setId(rs.getInt("id"));
					product.setName(rs.getString("name"));
					product.setProducingArea(rs.getString("producingArea"));
					product.setDescription(rs.getString("description"));
					product.setCreateTime(rs.getString("createTime"));
					product.setMarketPrice(rs.getDouble("marketPrice"));
					product.setSellPrice(rs.getDouble("sellPrice"));
					product.setProductAmount(rs.getInt("productAmount"));
					product.setPicture(rs.getString("picture"));
					product.setDiscount(rs.getInt("discount"));
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return product;
	}

	public List<Product> selectDiscountProducts() {
		List<Product> list = new ArrayList<>();
		Connection conn = jdbc.getConnection();
		try (PreparedStatement stmt = conn.prepareStatement("select * from product where discount=1");
			 ResultSet rs = stmt.executeQuery()) {
			fillProductListFromResultSet(rs, list);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	public List<Product> selectProductsByArea(String producingArea) {
		List<Product> list = new ArrayList<>();
		Connection conn = jdbc.getConnection();
		String sql = "select * from product where producingArea like?";
		try (PreparedStatement stmt = conn.prepareStatement(sql)) {
			stmt.setString(1, "%" + producingArea + "%");
			try (ResultSet rs = stmt.executeQuery()) {
				fillProductListFromResultSet(rs, list);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	public List<Product> selectProductsByName(String name) {
		List<Product> list = new ArrayList<>();
		Connection conn = jdbc.getConnection();
		String sql = "select * from product where name like?";
		try (PreparedStatement stmt = conn.prepareStatement(sql)) {
			stmt.setString(1, "%" + name + "%");
			try (ResultSet rs = stmt.executeQuery()) {
				fillProductListFromResultSet(rs, list);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	public boolean updateProduct(Product e) {
		Connection conn = jdbc.getConnection();
		String sql = "update product set name=?, producingArea=?, description=?, marketPrice=? where id=?";
		try (PreparedStatement stmt = conn.prepareStatement(sql)) {
			stmt.setString(1, e.getName());
			stmt.setString(2, e.getProducingArea());
			stmt.setString(3, e.getDescription());
			stmt.setDouble(4, e.getMarketPrice());
			stmt.setInt(5, e.getId());
			stmt.executeUpdate();
			return true;
		} catch (SQLException ex) {
			ex.printStackTrace();
			return false;
		}
	}
}
